package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.ktwlsoft.archivemanage.domain.business.repositoryService.AnnualReportRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.ArchiveUnitRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.businessInspect.BusinessInspectRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.guiBusiness.GuiBusinessGuidanceRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.guiBusiness.GuiGuidanceMendListRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.guiBusiness.GuidanceApplyRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.mend.MendListRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.register.AccountRegisterService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.sms.SmsService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.ArchiveUnit;
import com.ktwlsoft.archivemanage.domain.dao.entity.Report;
import com.ktwlsoft.archivemanage.domain.dao.entity.ReportDetail;
import com.ktwlsoft.archivemanage.domain.dao.entity.businessInspect.BusinessInspect;
import com.ktwlsoft.archivemanage.domain.dao.entity.guiBusiness.GuiBusinessGuidance;
import com.ktwlsoft.archivemanage.domain.dao.entity.guiBusiness.GuiGuidanceMendList;
import com.ktwlsoft.archivemanage.domain.dao.entity.guiBusiness.GuidanceApply;
import com.ktwlsoft.archivemanage.domain.dao.entity.mend.MendList;
import com.ktwlsoft.archivemanage.domain.dao.entity.register.AccountRegister;
import com.ktwlsoft.archivemanage.domain.dao.entity.sms.SendSms;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import com.ktwlsoft.archivemanage.service.utils.DateUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信发送业务服务
 *
 * @author Name on 2021/1/5 9:43
 * @version 1.0
 */
@Component
public class SmsBusinessService {

    /**短信服务接口实现*/
    private final SmsService smsService;

    /**账号注册业务层接口实现类*/
    private final AccountRegisterService accountRegisterService;

    /**指导申请业务层接口实现类*/
    private final GuidanceApplyRepositoryService guidanceApplyRepositoryService;

    /**业务指导业务层接口实现类*/
    private final GuiBusinessGuidanceRepositoryService repositoryService;

    /**业务指导整改业务层接口实现类*/
    private final GuiGuidanceMendListRepositoryService guidanceMendListRepositoryService;

    /**业务检查业务层接口实现类*/
    private final BusinessInspectRepositoryService inspectRepositoryService;

    /**检查整改*/
    private final MendListRepositoryService mendListRepositoryService;

    /**年度报送数据层服务类*/
    private final AnnualReportRepositoryService annualReportRepositoryService;

    /**立档单位业务层接口实现类*/
    private final ArchiveUnitRepositoryService archiveUnitRepositoryService;

    public SmsBusinessService(SmsService smsService, AccountRegisterService accountRegisterService, GuidanceApplyRepositoryService guidanceApplyRepositoryService, GuiBusinessGuidanceRepositoryService repositoryService, GuiGuidanceMendListRepositoryService guidanceMendListRepositoryService, BusinessInspectRepositoryService inspectRepositoryService, MendListService mendListService, MendListRepositoryService mendListRepositoryService, AnnualReportRepositoryService annualReportRepositoryService, ArchiveUnitRepositoryService archiveUnitRepositoryService) {
        this.smsService = smsService;
        this.accountRegisterService = accountRegisterService;
        this.guidanceApplyRepositoryService = guidanceApplyRepositoryService;
        this.repositoryService = repositoryService;
        this.guidanceMendListRepositoryService = guidanceMendListRepositoryService;
        this.inspectRepositoryService = inspectRepositoryService;
        this.mendListRepositoryService = mendListRepositoryService;
        this.annualReportRepositoryService = annualReportRepositoryService;
        this.archiveUnitRepositoryService = archiveUnitRepositoryService;
    }

    /**
      * 短信发送 on 2021/1/5 9:44
      *
      * @param model model
      * @return Result<?>
      */
    public Result<?> sendSms(SendSms model){
        return smsService.sendSms(model);
    }

    /**
     * 根据类型发送短信 on 2021/1/5 9:40
     *
     * @param id id
     * @param type 类型:1.注册申请提交,2.指导申请已提交提醒,3.指导申请未通过验证,4.指导申请已通过验证,
     *              5.业务指导待确认,6.业务指导已确认,7.新增指导整改,8.指导整改提交提醒,9.指导整改未通过验证,
     *              10.业务检查待确认,11.业务检查已确认，12.新增检查整改,13.检查整改已提交,14.检查整改未通过验证,
     *              15.年度报送已提交,16.年度报送报送未通过验证
     * @return Result<?>
     */
    public Result<?> sendBusinessSms(Long id,String phone,Integer type){
        if(phone == null || "".equals(phone)){
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("手机号码为空");
        }
        Account account = UserPrincipal.getCurrentUser();
        if(account == null){
            account = new Account();
        }
        switch (type){
            case 1:
                //注册申请提交
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                AccountRegister register = accountRegisterService.getById(id);
                if(register == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map = new HashMap<>();
                map.put("unitName", register.getUnitName());
                return smsService.sendSms("1","accountRegisterCommit",phone,map);
            case 2:
                //指导申请已提交提醒
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                GuidanceApply apply = guidanceApplyRepositoryService.getById(id);
                if(apply == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map1 = new HashMap<>();
                map1.put("unitName", apply.getUnitName());
                map1.put("applyTime", DateUtils.dateToStr(apply.getGuidanceTimeStart(),"yyyy-MM-dd") + " - " + DateUtils.dateToStr(apply.getGuidanceTimeEnd(),"yyyy-MM-dd"));
                return smsService.sendSms("1","guidanceApplyCommit",phone,map1);
            case 3:
                //指导申请未通过验证
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                GuidanceApply apply1 = guidanceApplyRepositoryService.getById(id);
                if(apply1 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                if(apply1.getState() >= 4){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据已通过验证");
                }
                //发送短信
                Map<String, String> map2 = new HashMap<>();
                map2.put("reason", apply1.getRemark());
                return smsService.sendSms("1","guidanceApplyWaitConfirm",phone,map2);
            case 4:
                //指导申请已通过验证
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                GuidanceApply apply2 = guidanceApplyRepositoryService.getById(id);
                if(apply2 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                if(apply2.getState() < 4){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据未通过验证");
                }
                //发送短信
                return smsService.sendSms("1","guidanceApplyConfirm",phone,null);
            case 5:
                //业务指导待确认
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                GuiBusinessGuidance guidance = repositoryService.getById(id);
                if(guidance == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                if(!"WAIT_CONFIRM".equals(guidance.getState())){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不是待确认状态");
                }
                //发送短信
                Map<String, String> map3 = new HashMap<>();
                map3.put("realName", account.getRealName());
                map3.put("GuidanceNum", guidance.getGuidanceNum());
                return smsService.sendSms("1","guidanceWaitConfirm",phone,map3);
            case 6:
                //业务指导已确认
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                GuiBusinessGuidance guidance1 = repositoryService.getById(id);
                if(guidance1 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                if(!"COMPLETE".equals(guidance1.getState()) && !"ARCHIVE".equals(guidance1.getState())){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不是确认状态");
                }
                //发送短信
                Map<String, String> map4 = new HashMap<>();
                map4.put("unitName", guidance1.getUnit());
                map4.put("GuidanceNum", guidance1.getGuidanceNum());
                return smsService.sendSms("1","guidanceConfirm",phone,map4);
            case 7:
                //新增指导整改
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                GuiGuidanceMendList mendList = guidanceMendListRepositoryService.getByGuidanceId(id);
                if(mendList == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                GuiBusinessGuidance guidance2 = repositoryService.getById(mendList.getGuidanceId());
                if(guidance2 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map5 = new HashMap<>();
                map5.put("realName", account.getRealName());
                map5.put("GuidanceNum", guidance2.getGuidanceNum());
                return smsService.sendSms("1","guidanceMendAdd",phone,map5);
            case 8:
                //指导整改提交提醒
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                GuiGuidanceMendList mendList1 = guidanceMendListRepositoryService.getById(id);
                if(mendList1 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                if(mendList1.getMendState() != 2){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不是提交状态");
                }
                GuiBusinessGuidance guidance3 = repositoryService.getById(mendList1.getGuidanceId());
                if(guidance3 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map6 = new HashMap<>();
                map6.put("organizationName", guidance3.getUnit());
                map6.put("GuidanceNum", guidance3.getGuidanceNum());
                return smsService.sendSms("1","guidanceMendCommit",phone,map6);
            case 9:
                //指导整改未通过验证
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                GuiGuidanceMendList mendList2 = guidanceMendListRepositoryService.getById(id);
                if(mendList2 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                if(mendList2.getMendState() == 4){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据已通过验证");
                }
                GuiBusinessGuidance guidance4 = repositoryService.getById(mendList2.getGuidanceId());
                if(guidance4 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map7 = new HashMap<>();
                map7.put("GuidanceNum", guidance4.getGuidanceNum());
                return smsService.sendSms("1","guidanceMendVerificationFail",phone,map7);
            case 10:
                //业务检查待确认
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                BusinessInspect inspect = inspectRepositoryService.getById(id);
                if(inspect == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
//                if(!BusinessInspect.state.WAIT_CONFIRM.equals(inspect.getState())){
//                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不是待确认状态");
//                }
                //发送短信
                Map<String, String> map8 = new HashMap<>();
                map8.put("realName", account.getRealName());
                map8.put("markAndFullMark", inspect.getMark() + "分/" + inspect.getFullMark() + "分");
                map8.put("GuidanceNum", inspect.getGuidanceNum());
                return smsService.sendSms("1","inspectWaitConfirm",phone,map8);
            case 11:
                //业务检查已确认
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                BusinessInspect inspect1 = inspectRepositoryService.getById(id);
                if(inspect1 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
//                if(!BusinessInspect.state.COMPLETE.equals(inspect1.getState())){
//                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不是已确认状态");
//                }
                //发送短信
                Map<String, String> map9 = new HashMap<>();
                map9.put("unitName", inspect1.getUnitName());
                map9.put("GuidanceNum", inspect1.getGuidanceNum());
                return smsService.sendSms("1","inspectConfirm",phone,map9);
            case 12:
                //新增检查整改
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                MendList mendList3 = mendListRepositoryService.getByInspectId(id);
                if(mendList3 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map10 = new HashMap<>();
                map10.put("realName", account.getRealName());
                map10.put("markAndFullMark", mendList3.getMark() + "分/" + mendList3.getFullMark() + "分");
                map10.put("GuidanceNum", mendList3.getGuidNum());
                return smsService.sendSms("1","inspectMendAdd",phone,map10);
            case 13:
                //检查整改已提交
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                MendList mendList4 = mendListRepositoryService.getByInspectId(id);
                if(mendList4 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map11 = new HashMap<>();
                map11.put("unitName", mendList4.getArchiveUnit());
                map11.put("GuidanceNum", mendList4.getGuidNum());
                map11.put("markAndFullMark", mendList4.getMark() + "分/" + mendList4.getFullMark() + "分");
                return smsService.sendSms("1","inspectMendCommit",phone,map11);
            case 14:
                //检查整改未通过验证
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                MendList mendList5 = mendListRepositoryService.getByInspectId(id);
                if(mendList5 == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map12 = new HashMap<>();
                map12.put("markAndFullMark", mendList5.getMark() + "分/" + mendList5.getFullMark() + "分");
                map12.put("GuidanceNum", mendList5.getGuidNum());
                return smsService.sendSms("1","inspectMendVerificationFail",phone,map12);
            case 15:
                //年度报送已提交
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                ReportDetail detail = annualReportRepositoryService.findAnnualReportInfo(id);
                if(detail == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                ArchiveUnit archiveUnit = archiveUnitRepositoryService.getById(detail.getUnitId());
                if(archiveUnit == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map13 = new HashMap<>();
                map13.put("organizationName", archiveUnit.getUnitName());
                map13.put("writeDate", String.valueOf(detail.getWriteDate().getValue()));
                return smsService.sendSms("1","reportYearCommit",phone,map13);
            case 16:
                //年度报送报送未通过验证
                if(id == null){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("id为空");
                }
                Report report = annualReportRepositoryService.findAnnualReportDetailById(id);
                if(report == null){
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
                }
                //发送短信
                Map<String, String> map14 = new HashMap<>();
                map14.put("ReportId", report.getReportId());
                return smsService.sendSms("1","reportVerificationFail",phone,map14);
                default:
                    return Result.body(id, ResultType.CHECK_ERROR).withMessage("该类型不存在");
        }
    }
}